package org.ares.app.demo.actions;


import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Resource;
import org.ares.app.demo.daos.AuthService;
import org.ares.app.demo.daos.CarDao;
import org.ares.app.demo.daos.CaruserDao;
import org.ares.app.demo.daos.LoginDao;
import org.ares.app.demo.daos.ParamDao;
import org.ares.app.demo.daos.PeccancyDao;
import org.ares.app.demo.daos.PectypeDao;
import org.ares.app.demo.daos.UserDao;
import org.ares.app.demo.entities.Loginuser;
import org.ares.app.demo.entities.SUser;
import org.ares.app.demo.models.b;
import org.ares.app.demo.models.d;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping({"/api/v2"})
public class BizApiXAction {
    @Value("${server.port}")
    int port;
    @Value("${server.context-path}")
    String contextPath;
    final String ip = "localhost";
    final String prefix = "/simusand/api/v2/";
    final ObjectMapper mapper = new ObjectMapper();
    static final Logger log = LoggerFactory.getLogger(BizApiXAction.class);
    @Resource
    AuthService auth;
    @Resource
    UserDao userdao;
    @Resource
    ParamDao paramdao;
    @Resource
    PeccancyDao peccancydao;
    @Resource
    CarDao cardao;
    @Resource
    CaruserDao caruserdao;
    @Resource
    LoginDao logindao;
    @Resource
    PectypeDao pecdao;
    @Resource
    RestTemplate restTemplate;

    public BizApiXAction() {
    }

    @RequestMapping({"/user/register"})
    public Map<String, Object> register(@RequestBody d model) {
        HashMap var2 = new HashMap();
        SUser var3 = new SUser();
        BeanUtils.copyProperties(model, var3);
        this.userdao.save(var3);
        var2.put("msg", "success");
        return var2;
    }

    @RequestMapping({"/set_etc_blacklist"})
    public Map<String, Object> jsonAry(@RequestBody b param) {
        String var2 = param.a();
        SUser var3 = this.userdao.findByUsername(var2);
        if (var3 == null) {
            throw new RuntimeException("username is invalid");
        } else if (StringUtils.isEmpty(var3.getRole())) {
            throw new RuntimeException("user is not approve");
        } else if (!this.auth.auth(var3.getRole(), "set_etc_blacklist")) {
            throw new RuntimeException("user is not auth");
        } else {
            String var4 = "http://localhost:" + this.port + this.contextPath + "/simusand/api/v2/" + "set_etc_blacklist";
            HttpHeaders var5 = new HttpHeaders();
            var5.setContentType(MediaType.APPLICATION_JSON);
            HttpEntity var6 = new HttpEntity(param, var5);
            log.info("[to sand table params]--" + var6);
            Map var7 = null;

            try {
                var7 = this.getSandTableData(var4, var6);
                return var7;
            } catch (Exception var9) {
                throw new RuntimeException("access sand_table failed,maybe param lose or timeout");
            }
        }
    }

    @RequestMapping({"/{app_req_url}"})
    public Map<String, Object> transport(@PathVariable String app_req_url, @RequestBody Map<String, String> param) {
        String var3 = "UserName";
        if (param.containsKey(var3) && !StringUtils.isEmpty(param.get(var3))) {
            String var4 = (String)param.get("UserName") + "";
            SUser var5 = this.userdao.findByUsername(var4);
            if (var5 == null) {
                throw new RuntimeException("username is invalid");
            } else if (StringUtils.isEmpty(var5.getRole())) {
                throw new RuntimeException("user is not approve");
            } else if (!this.auth.auth(var5.getRole(), app_req_url)) {
                throw new RuntimeException("user is not auth");
            } else {
                HashMap var6 = new HashMap();
                boolean var7 = false;
                byte var9 = -1;
                switch(app_req_url.hashCode()) {
                    case -2071742984:
                        if (app_req_url.equals("get_peccancy_type")) {
                            var9 = 4;
                        }
                        break;
                    case -1935284990:
                        if (app_req_url.equals("get_car_info")) {
                            var9 = 3;
                        }
                        break;
                    case -1226971222:
                        if (app_req_url.equals("get_all_car_peccancy")) {
                            var9 = 2;
                        }
                        break;
                    case 640305755:
                        if (app_req_url.equals("get_all_user_info")) {
                            var9 = 0;
                        }
                        break;
                    case 1119737420:
                        if (app_req_url.equals("get_car_peccancy")) {
                            var9 = 1;
                        }
                        break;
                    case 1928198645:
                        if (app_req_url.equals("user_login")) {
                            var9 = 5;
                        }
                }

                String var11;
                switch(var9) {
                    case 0:
                        var6.put("ROWS_DETAIL", this.caruserdao.findAll());
                        var7 = true;
                        break;
                    case 1:
                        String var10 = "" + (String)param.get("carnumber");
                        if (StringUtils.isEmpty(param.get("carnumber"))) {
                            throw new RuntimeException("Param carnumber is must ");
                        }

                        var6.put("ROWS_DETAIL", this.peccancydao.findByCarnumber(var10));
                        var7 = true;
                        break;
                    case 2:
                        var6.put("ROWS_DETAIL", this.peccancydao.findAll());
                        var7 = true;
                        break;
                    case 3:
                        var6.put("ROWS_DETAIL", this.cardao.findAll());
                        var7 = true;
                        break;
                    case 4:
                        var6.put("ROWS_DETAIL", this.pecdao.findAll());
                        var7 = true;
                        break;
                    case 5:
                        var11 = (String)param.get("UserPwd") + "";
                        if (StringUtils.isEmpty(param.get("UserPwd"))) {
                            throw new RuntimeException("Param UserPwd is must ");
                        }

                        Loginuser var12 = this.logindao.findByUsernameAndUserpwd(var4, var11);
                        if (var12 == null) {
                            throw new RuntimeException("UserPwd is invalid");
                        }

                        var6.put("UserRole", var12.getUserrole());
                        var7 = true;
                }

                if (var7) {
                    var6.put("RESULT", "S");
                    var6.put("ERRMSG", "成功");
                    return var6;
                } else {
                    String var8 = "http://localhost:" + this.port + this.contextPath + "/simusand/api/v2/" + app_req_url;
                    System.out.println(var8);
                    log.info("[to sand table url]--" + var8);
                    HttpHeaders var15 = new HttpHeaders();
                    var15.setContentType(MediaType.APPLICATION_JSON);
                    HttpEntity var14 = new HttpEntity(param, var15);
                    log.info("[to sand table params]--" + var14);
                    var11 = null;

                    try {
                        Map var16 = this.getSandTableData(var8, var14);
                        return var16;
                    } catch (Exception var13) {
                        throw new RuntimeException("access sand_table failed,maybe param lose or timeout");
                    }
                }
            }
        } else {
            throw new org.ares.app.demo.common.exception.a();
        }
    }

    String getSandUrl(String req_url) {
        return req_url;
    }

    Map<String, Object> getSandTableData(String full_url, HttpEntity param) throws Exception {
        new HashMap();
        Map var3 = (Map)this.restTemplate.postForObject(full_url, param, Map.class, new Object[0]);
        log.info("[from sand table data]--" + var3);
        return var3;
    }

    void buildResultOfSand(Map<String, Object> m) {
        if (m == null) {
            throw new RuntimeException("sand_table response is null");
        } else {
            String var2 = null;
            if (m.containsKey("result")) {
                var2 = m.get("result") + "";
                if ("ok".equals(var2)) {
                    m.put("RESULT", "S");
                    m.put("ERRMSG", "成功");
                }

                if ("failed".equals(var2)) {
                    m.put("RESULT", "F");
                    m.put("ERRMSG", "失败");
                }

                m.remove("result");
            } else {
                m.put("RESULT", "S");
                m.put("ERRMSG", "成功");
            }
        }
    }
}
